package com.company.二叉树;

/**
 * @param
 * @author jianjian.zhang
 * @date 2022/07/24 17:46
 * @return
 * @description: 二叉树的前序遍历
 */

public class 二叉树的前序遍历 {

    int[] result = new int[6];
    int i = 0;
    public int[] preorderTraversal(TreeNode root) {

        if (root == null) {
            return new int[0];
        }

        result[i] = root.getData();
        i++;
        preorderTraversal(root.left);
        preorderTraversal(root.right);

        return result;
    }

    public static void main(String[] args) {
        二叉树的前序遍历 preorder = new 二叉树的前序遍历();
        int[] datas = {72, 37, 29, 55, 51, 80};
        // 创建二叉树
        CreateTree_01 biTree = CreateTree_01.createBiTree(datas);
        System.out.println("root ===== "+biTree.getRoot());

        int[] ints = preorder.preorderTraversal(biTree.root);
        for (int i = 0; i < ints.length; i++) {
            System.out.print(ints[i] +" ");
        }

    }
}
